home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 76623,2065@compuserve.com (Bobby Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q]Virtualizing/Deriving
- Date: 17 Apr 1996 13:42:14 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4l2sfm$ln6@dub-news-svc-2.compuserve.com>
- References: <40.91023.1613@channel1.com> <4knlec$lno@qualcomm.com>
- Reply-To: 76623,2065@compuserve.com (Bobby Martin)
- NNTP-Posting-Host: dd21-037.compuserve.com
- X-Newsreader: IBM NewsReader/2 v1.03
-
- In <4knlec$lno@qualcomm.com>, drew@qualcomm.com (Drew Eckhardt) writes:
- >In article <40.91023.1613@channel1.com>,
- >Dspse Bedford <dspse.bedford@channel1.com> wrote:
- >>I just started using C++ so bare with me.
- >>
- >>Lets say I have a base class, BASE, with lots and lots of operators and
- >>member functions virtualized, like 10,000. Now I create 100 derived classes
- >>with BASE as their parent. The first 99 derived classes use all of the
- >>operators and member function which exist in BASE, of course, if they need
- >>to redefine any they will. The last derived class does not use some of the
- >>operators defined, lets say 2 out of 10,000. Now if I don't define these 2
- >>operators, the compiler will go pick up the parent definition if a user of
- >>the last derived class attempts to use any of these two operators.
- >> What can I do such that the compiler will give an error to the user? I
- >>do not want to create another base class if possible.
- >
- >You can declare the functions you don't want called as private, and fail
- >to define them. Ie, given
- >
- > class foo : public base {
- > public:
- > // stuff
- >
- > private:
- > void dont_call ();
- > };
- >
- >a user will get an error message if he attempts to call dont_call
- >(scope is resolved before overloading, so this will keep the user
- >from getting at a dont_call function with _any_ type signature in
- >the base class).
- >
- >--
- ><a href="http://www.poohsticks.org/drew/">Home Page</a>
- >Four boxes : soap, ballot, jury, ammo. | Work: drew@Qualcomm.COM
- >Use in that order. | Play: drew@PoohSticks.ORG
-
- I wouldn't try this, if you have declared the methods as virtual then you
- must be calling them from a pointer to the base class. Any code compiled with
- a pointer to the base class will know nothing about the method being redeclared
- private. I believe the lack of a definition of a virtual function should cause
- a link error if that virtual function is ever used, *even if only from a base
- class pointer*.
- If you need some methods to not be available to some children, you
- should solve it as the design problem it is, not as a coding problem. Leave
- those methods out of the class BASE, and add a subclass of BASE that defines
- those methods (maybe called BaseAddOn). Then inherit any of the children that
- need those methods from BaseAddOn and inherit the other children from BASE.
- Any function that needs only Base's functionality should use a BASE* to receive
- objects, and any function that also needs the extra methods should use a
- BaseAddOn* to receive objects.
-
- Hope that helps,
- Bobby Martin
-